#include "tftp_server.h"
#include "app_3axis_output.h"
#include "eeprom.h"

#define AXIS_BUF_BYTE_NUM     (AXIS_BUFFER_SIZE * 6)

extern volatile axis_type_t axis_buf[AXIS_BUFFER_SIZE];
static uint16_t raw_data_number;

static void test_data(void)
{
    uint16_t i;
    axis_type_t data = {
        1,1,1
    };
    
    for(i = 0; i < AXIS_BUFFER_SIZE; i++)
    {
        axis_buf[i] = data;
        data.axis_x++;
        data.axis_y++;
        data.axis_z++;
    }
}

static void get_raw_data_number(void)
{
    raw_data_number = eeprom_read_half_word(EEPROM_3AXIS_SAMPLE_WINDOW_ADDR);
}

static uint16_t raw_data_read(uint8_t blk, uint8_t* buffer)
{
    uint16_t i, count;
    uint8_t* raw_data_buffer = (void*)axis_buf;
 
    count = (blk - 1) * 512;
 
    for(i = 0; i < 512; i++)
    {
         if(count + i >= (raw_data_number * 6))
             break;
         
         buffer[i] = raw_data_buffer[count + i];
    }
    
    return i;
}

bool _raw_data_read_cmd_open(char* name, uint8_t mode)
{   
    if (mode != TFTP_RRQ) return false;
 
    if (0 == strcmp(name, "raw_data")) {
        get_raw_data_number();
        return true;
    } else {
        return false;
    }
}

uint16_t (_raw_data_read_cmd_read)(uint8_t blk, uint8_t* buffer)
{
    if (blk >= 1) {
        return raw_data_read(blk, buffer);
    }else{
        return 0;
    }
}

void (_raw_data_read_cmd_write)(uint8_t blk, uint8_t* buffer, uint16_t len)
{
     
}

void (_raw_data_read_cmd_close)(void)
{

}

const tftp_server_plugin_t tftp_server_plugin_raw_data_read_cmd = {
    _raw_data_read_cmd_open,
    _raw_data_read_cmd_read,
    _raw_data_read_cmd_write,
    _raw_data_read_cmd_close,
};


